1     //------------------------------------------------------------------------------
2     /// <copyright from='1997' to='2001' company='Microsoft Corporation'>
3     ///    Copyright (c) Microsoft Corporation. All Rights Reserved.
4     ///
5     ///    This source code is intended only as a supplement to Microsoft
6     ///    Development Tools and/or on-line documentation.  See these other
7     ///    materials for detailed information regarding Microsoft code samples.
8     ///
9     /// </copyright>
10    //------------------------------------------------------------------------------
11    namespace Microsoft.Samples.WinForms.Cs.PrintingExample1 {
12        using System;
13        using System.ComponentModel;
14        using System.Windows.Forms;
15        using System.Drawing;
16        using System.Drawing.Printing;
17        using System.IO;
18
19        public class PrintingExample1 : System.Windows.Forms.Form {
20            /// <summary>
21            ///    Required designer variable.
22            /// </summary>
23            private System.ComponentModel.Container components;
24            protected internal System.Windows.Forms.Button printButton;
25
26            private Font printFont;
27            private StreamReader streamToPrint;
28
29
30            public PrintingExample1() {
31
32                //
33                // Required for Windows Form Designer support
34                //
35                InitializeComponent();
36
37                //
38                // Add any constructor code after InitializeComponent call
39                //
40
41                // Wire up event handler for print button
42                printButton.Click += new System.EventHandler(printButton_Click);
43            }
44
45            /// <summary>
46            ///    Clean up any resources being used.
47            /// </summary>
48            public override void Dispose()
49            {
50                base.Dispose();
51                components.Dispose();
52            }
53
54            //Event fired when the user presses the print button
55            private void printButton_Click(object sender, EventArgs e) {
56                try {
57
58                    streamToPrint = new StreamReader ("PrintMe.Txt");
59                    try {
60                        printFont = new Font("Arial", 10);
61                        PrintDocument pd = new PrintDocument(); //Assumes the default printer
62                        pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
63                        pd.Print();
64                    } finally {
65                        streamToPrint.Close() ;
66                    }
67
68                } catch(Exception ex) {
69                    MessageBox.Show("An error occurred printing the file - " + ex.Message);
70                }
71            }
72
73            //Event fired for each page to print
74            private void pd_PrintPage(object sender, PrintPageEventArgs ev) {
75                float lpp = 0 ;
76                float yPos =  0 ;
77                int count = 0 ;
78                float leftMargin = ev.MarginBounds.Left;
79                float topMargin = ev.MarginBounds.Top;
80                String line=null;
81
82                //Work out the number of lines per page
83                //Use the MarginBounds on the event to do this
84                lpp = ev.MarginBounds.Height  / printFont.GetHeight(ev.Graphics) ;
85
86                //Now iterate over the file printing out each line
87                //NOTE WELL: This assumes that a single line is not wider than the page width
88                //Check count first so that we don't read line that we won't print
89                while (count < lpp && ((line=streamToPrint.ReadLine()) != null)) {
90                    yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
91
92                    ev.Graphics.DrawString (line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
93
94                    count++;
95                }

